﻿using StationDemo.Properties;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using StationDemo.FrameUI;
using CommonDlg;
using BaseDll;
using CommonTools;
using ModuleUnit;
using Language;
using DBForm;
namespace StationDemo
{
    public partial class SystemSet : UIPage
    {
        public SystemSet()
        {
            InitializeComponent();

        }

        private void SystemSet_Load(object sender, EventArgs e)
        {
            foreach (int myCode in Enum.GetValues(typeof(UIStyle)))
            {
                string strName = Enum.GetName(typeof(UIStyle), myCode);//获取名称
                string strVaule = myCode.ToString();//获取值
            }

            var usermanager = new Form_UserManager { Dock = DockStyle.Fill, TopLevel = false };
            usermanager.Show();
            pageUser.Controls.Add(usermanager);

            var RunSetting = new Form_RunSetting() { Dock = DockStyle.Fill, TopLevel = false };
            RunSetting.Show();
            pageRunSet.Controls.Add(RunSetting);

            var DeviceManage = new OtherDevice.DeviceManage() { Dock = DockStyle.Fill, TopLevel = false };
            DeviceManage.Show();
            tab_DeviceManage.Controls.Add(DeviceManage);

            var moduleManage = new Form_ModuleManage() { Dock = DockStyle.Fill, TopLevel = false };
            moduleManage.Show();
            tab_ModuleControl.Controls.Add(moduleManage);


            var alarm = new Form_DesignAlarmInfo() { Dock = DockStyle.Fill, TopLevel = false };
            alarm.Show();
            Page_Alarm.Controls.Add(alarm);

            var Light = new Form_StationInfo() { Dock = DockStyle.Fill, TopLevel = false };
            Light.Show();
            tab_LightControl.Controls.Add(Light);

            var softVersion = new Form_VisionType() { Dock = DockStyle.Fill, TopLevel = false };
            softVersion.Show();
            tab_SoftVersion.Controls.Add(softVersion);


            sys.g_eventRightChanged += ChangedUserRight;
            GlobalVariable.g_eventStationStateChanged += GlobalVariable_g_eventStationStateChanged;
            GlobalVariable_g_eventStationStateChanged(GlobalVariable.g_StationState);
        }
        public void ChangedUserRight(User CurrentUser)
        {
            GlobalVariable_g_eventStationStateChanged(GlobalVariable.g_StationState);
        }

        private void GlobalVariable_g_eventStationStateChanged(StationState currState)
        {
            if (InvokeRequired)
                this.BeginInvoke(new Action(() => GlobalVariable_g_eventStationStateChanged(currState)));
            else
            {
                if ((int)sys.g_User._userRight >= (int)UserRight.Debug && (GlobalVariable.g_StationState == StationState.StationStateEmg || GlobalVariable.g_StationState == StationState.StationStateStop))
                {
                    tab_DeviceManage.Enabled = true;
                    tab_ModuleControl.Enabled = true;

                }
                else
                {
                    tab_DeviceManage.Enabled = false;
                    tab_ModuleControl.Enabled = false;

                }
            }

        }

        private void BtnOpenPathSel_Click(object sender, EventArgs e)
        {
            //FileDialog ofd = new FileDialog();
            //ofd.Filter = " *.* (*.*)|*.*";
            //if (ofd.ShowDialog() == DialogResult.OK)
            //{
            //    txtOfProductFilePath.Text = ofd.FileName;
            //}

            FolderDialog openFolder = new FolderDialog();
            if (openFolder.DisplayDialog() == DialogResult.OK)
            {
                txtOfProductFilePath.Text = openFolder.Path.ToString();
      
            }
            else
            {
                UIMessageBox.ShowError( "你没有选择目录");
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtOfProductFilePath.Text == null || txtOfProductFilePath.Text == "")
            {
                UIMessageBox.ShowError($"路径不合法");
                return;
            } 
         if( UIMessageBox.ShowAsk($"是否要更换参数文件路径; 新的路径为：{txtOfProductFilePath.Text} 更换完毕 App 将会重启"))
            {
                ParamSetMgr.GetInstance().CurrentWorkDir = txtOfProductFilePath.Text;
                Settings.Default.Save();
                ConfigToolMgr.GetInstance().SaveProductFile();
               
                System.Environment.Exit(0);
            }
        }

        private void uiTitlePanelOfProductFilesDir_Load(object sender, EventArgs e)
        {
            txtOfProductFilePath.Text = ParamSetMgr.GetInstance().CurrentWorkDir;
        }



        private void cbb_Control_ValueChanged(object sender, bool value)
        {

        }

        private void btn_ChangeLanguage_Click(object sender, EventArgs e)
        {
            Form_LanguageChange form_LanguageChange = new Form_LanguageChange();
            form_LanguageChange.Show();
        }

        private void cbb_Language_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbb_Language.SelectedIndex == 0)
                LanguageHelp.GetInstance().languageData.Type_LanguageType = LanguagesType.中文;
            else
                LanguageHelp.GetInstance().languageData.Type_LanguageType = LanguagesType.English;
            LanguageHelp.GetInstance().Funtion_TransLate();
            LanguageHelp.GetInstance().Save();
        }
        bool b_First = true;
        private void uiTabControlMenu1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(uiTabControlMenu1.SelectedTab.Text=="数据查询"&&b_First)
            {
                b_First = false;
                var deviceData = new DBForm.页面.DBForm_主页面() { Dock = DockStyle.Fill };
                deviceData.Show();
                tabReport.Controls.Add(deviceData);

            }
        }
    }
}
